package com.gxd.service.impl;

import com.gxd.dao.CartDao;
import com.gxd.dao.impl.CartDaoImpl;
import com.gxd.domain.Cart;
import com.gxd.domain.Goods;
import com.gxd.service.CartService;
import com.gxd.service.GoodsService;

import java.util.List;

public class CartServiceImpl implements CartService {
    CartDao cartDao = new CartDaoImpl();
    @Override
    public Cart findUidAndPid(int uid, int goodsId) {
        return cartDao.findUidAndPid(uid,goodsId);
    }

    @Override
    public void insert(Cart cart) {
    cartDao.insert(cart);
    }

    @Override
    public void update(Cart cart) {
        cartDao.update(cart);
    }

    @Override
    public List<Cart> findUid(int id) {
        List<Cart> carts=cartDao.findUid(id);
        GoodsService goodsService = new GoodsServiceImpl();
        if(carts!=null&&carts.size()>0){
            for (Cart cart : carts) {
                Goods goodById = goodsService.findGoodById(cart.getPid());
                cart.setGoods(goodById);
            }
        }

        return carts;
    }

    @Override
    public void deleteCart(int id, int pid) {
        cartDao.delete(id,pid);
    }

    @Override
    public void clearCart(int id) {
        cartDao.clearCart(id);
    }
}
